package com.mainworks.commons.exception;

import com.mainworks.commons.UnifyResponse;
import com.mainworks.commons.utils.StringCustomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;

/**
 * 全局异常处理类
 * @author  HuangMinJie
 */
@RestControllerAdvice
public class OverallSituationException {
    Logger logger = LoggerFactory.getLogger(OverallSituationException.class);
    @ExceptionHandler(SQLException.class)
    public UnifyResponse sqlException(SQLException e){
        logger.error(StringCustomUtils.DATETIME.format(new Date()),"SQLException 异常",e);
        e.printStackTrace();
        if (e instanceof SQLIntegrityConstraintViolationException){
            return UnifyResponse.error("该数据有关联数据,操作失败");
        }
        return UnifyResponse.error("数据库异常,操作失败");
    }

}
